package top.xinsix.est.service.impl;
import top.xinsix.est.dto.ModelDto;
import top.xinsix.est.mapper.ModelMapper;
import top.xinsix.est.pojo.Model;
import top.xinsix.est.req.ModelReq;
import top.xinsix.est.service.ModelService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import top.xinsix.est.dto.Page;
import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;
import top.xinsix.est.tools.oss.OssConfig;

import javax.annotation.Resource;

@Service
public class ModelServiceImpl implements ModelService {

    @Resource
    private ModelMapper modelMapper;

    @Resource
    private OssConfig ossConfig;

    public Model getModelById(Long id){
        return modelMapper.getModelById(id);
    }

    public List<Model>	getModelListByMap(Map<String,Object> param){
        return modelMapper.getModelListByMap(param);
    }

    public Integer getModelCountByMap(Map<String,Object> param){
        return modelMapper.getModelCountByMap(param);
    }

    public Integer qdtxAddModel(Model model){
            model.setCreatdTime(new Date());
            return modelMapper.insertModel(model);
    }

    public Integer qdtxModifyModel(Model model){
        model.setUpdatedTime(new Date());
        return modelMapper.updateModel(model);
    }

    public Integer qdtxDeleteModelById(Long id){
        return modelMapper.deleteModelById(id);
    }

    public Integer qdtxBatchDeleteModel(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return modelMapper.batchDeleteModel(param);
    }

    public Page<Model> queryModelPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = modelMapper.getModelCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Model> modelList = modelMapper.getModelListByMap(param);
        page.setList(modelList);
        return page;
     }

    public Page<ModelDto> search(ModelReq modelReq) {
        //获取总条数
        Integer total=modelMapper.getCountByModelReq(modelReq);
        //int pageNo, int pageSize,Integer total
        Page<ModelDto> page=new Page<ModelDto>(modelReq.getPageNo(),modelReq.getPageSize(),total);
        modelReq.setBeginPos(page.getBeginPos());
        modelReq.setPageNo(page.getPageNo());
        modelReq.setPageSize(page.getPageSize());
        List<ModelDto> modelDtoList=modelMapper.getModelListByModelReq(modelReq);
        //处理faceImg
        formatModelDtoList(modelDtoList);
        page.setList(modelDtoList);
        return page;
    }
    //图片格式转换
    private void formatModelDtoList(List<ModelDto> modelDtoList){
        for(ModelDto modelDto : modelDtoList){
            formatModelDto(modelDto);
        }
    }
    //图片格式转换
    private void formatModelDto(ModelDto modelDto){
        if(modelDto!=null&&modelDto.getFaceImg()!=null){
            modelDto.setFaceImg(ossConfig.getOssWebUrl()+modelDto.getFaceImg());
        }
        if(modelDto!=null&&modelDto.getContentImg()!=null){
            modelDto.setContentImg(ossConfig.getOssWebUrl()+modelDto.getContentImg());
        }
    }

    public ModelDto getModelDtoById(Long id) {
        Model model = modelMapper.getModelById(id);
        ModelDto modelDto = createModelDto(model);
        //调用封面和内容图片格式的方法
        formatModelDto(modelDto);
        return modelDto;
    }
    //将Model对象转换为ModelDto对象
    public ModelDto createModelDto(Model model){
        ModelDto modelDto = new ModelDto(model);
        return modelDto;
    }
}
